using UnityEngine;

public class BakedShadow : MonoBehaviour
{
    public int _VolumeSize = 100;
    public RenderTexture _ShadowMap3D = null;
    //public Texture3D _ShadowMap3D = null;
    public ComputeShader shadowCompute;

    public bool _doBake = false;

    public void Update()
    {
        if (_doBake)
        {
            UnityEngine.Debug.Log("DoBakedShadow");
            DoBakedShadow();

            _doBake = false;
        }
    }

    public void DoBakedShadow()
    {
        _ShadowMap3D = new RenderTexture(_VolumeSize, _VolumeSize, 0);
        _ShadowMap3D.dimension = UnityEngine.Rendering.TextureDimension.Tex3D;
        _ShadowMap3D.name = "_IGVI_ShadowMap3D";
        _ShadowMap3D.volumeDepth = _VolumeSize;
        _ShadowMap3D.enableRandomWrite = true;
        _ShadowMap3D.format = RenderTextureFormat.ARGBHalf;
        _ShadowMap3D.wrapMode = TextureWrapMode.Clamp;
        _ShadowMap3D.filterMode = FilterMode.Bilinear;
        
        int kernel = shadowCompute.FindKernel("CSMain");
        
        shadowCompute.SetTexture(kernel, "_Result", _ShadowMap3D);
        shadowCompute.SetInt("_VolumeSize", _VolumeSize);
        shadowCompute.SetVector("_VolumeOrigin", Vector4.zero);

        int groupSize = Mathf.CeilToInt(_VolumeSize / 10f);
        shadowCompute.Dispatch(kernel, groupSize, groupSize, groupSize);
    }
    
    
    public void OnDrawGizmos()
    {
        Vector3 size = new Vector3(_VolumeSize,_VolumeSize,_VolumeSize);
        Gizmos.DrawWireCube(new Vector3(50,50,50), size);
    }
}
